package com.ruyuan.commodity.utils;

import com.google.common.collect.Maps;
import net.sf.cglib.beans.BeanMap;

import java.util.Map;

public class MapTools {


    /**
     * 将Object对象里面的属性和值转化成Map对象
     *
     * @param obj
     * @return
     * @throws IllegalAccessException
     */
//    public static Map<String, Object> objectToMap(Object obj) throws IllegalAccessException {
//        Map<String, Object> map = new HashMap<String,Object>();
//        Class<?> clazz = obj.getClass();
//        for (Field field : clazz.getDeclaredFields()) {
//            field.setAccessible(true);
//            String fieldName = field.getName();
//            Object value = field.get(obj);
//            map.put(fieldName, value);
//        }
//        return map;
//    }

    /**
     * 获取参数不为空值
     *
     * @param value defaultValue 要判断的value
     * @return value 返回值
     */
    public static <T> T nvl(T value, T defaultValue)
    {
        return value != null ? value : defaultValue;
    }

    /**
     * 将Object对象里面的属性和值转化成Map对象
     * @param obj 要转换的对象
     * @return map
     */
    public static Map<String, Object> objectToMap(Object obj) {
        Map<String, Object> map = Maps.newHashMap();
        if (obj != null) {
            BeanMap beanMap = BeanMap.create(obj);
            for (Object key : beanMap.keySet()) {
                map.put((String) key, beanMap.get(key));
            }
        }
        return map;
    }

}